# -*- coding: utf-8 -*-
import collections
from xml.dom import minidom
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)


def createXml(list):
    dom=minidom.Document()
    urlset_node=dom.createElement('urlset')
    dom.appendChild(urlset_node)


    for arc in list:
        # 用DOM对象创建元素子节点
        dom.encoding="utf-8"
        url_node=dom.createElement('url')
        # 用父节点对象添加元素子节点
        urlset_node.appendChild(url_node)
        for k,v in arc.items():
          if k == 'data':
            data_node=dom.createElement('data')
            url_node.appendChild(data_node)
            display_node=dom.createElement('display')
            data_node.appendChild(display_node)
            for ik,iv in v.items():
                ik_node=dom.createElement(ik)
                display_node.appendChild(ik_node)
                iv_node=dom.createTextNode(iv)
                ik_node.appendChild(iv_node)
          else:
            print(k+"----url---"+v)
            k_node=dom.createElement(k)
            url_node.appendChild(k_node)
            v_node=dom.createTextNode(v)
            k_node.appendChild(v_node)
                
    return dom.toprettyxml(encoding = 'UTF-8')

if __name__ == '__main__':
    mydic=collections.OrderedDict()
    mydic['loc']='http://47.104.9.64/api/today'
    mydic['lastmod']='2018-08-24'
    mydic['changefreq']='daily'
    mydic['priority']='1.0'
    dics=collections.OrderedDict()
    dics['action']='add'
    dics['source_id']='2'
    dics['source_name']='6df7bbe2-c4a6-3134-af20-705ba5161d2c'
    dics['source_type']='news'
    dics['update_time']='1503845435'
    dics['tags']='非摩羯'
    dics['title']='肉陀大臭屁'
    dics['abstract']='谁是大臭屁'
    dics['content']='谁是大臭屁'
    mydic['data']=dics
    print(mydic)
    print(createXml([mydic,mydic]))
